package com.ilovepostcard.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ilovepostcard.R;
import com.ilovepostcard.utils.StringUtils;

public class PostcardMenuItem extends RelativeLayout {

    private View container;
    private ImageView iconView;
    private TextView textView;
    private int type;
    private boolean selected = false;
    private OnMenuClickListener onMenuClickListener;
    
    public static interface OnMenuClickListener {
        void onClickMenu();
    }

    public PostcardMenuItem(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        inflate();
        loadAttrs(attrs);
    }

    public PostcardMenuItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflate();
        loadAttrs(attrs);
    }

    public PostcardMenuItem(Context context) {
        super(context);
        inflate();
    }
    
    public void showText(boolean show)
    {
    	 textView.setVisibility(show ? View.VISIBLE:View.GONE);
    }

    private void inflate() {
        View view = View.inflate(getContext(), R.layout.postcard_menu_item, this);
        container = view.findViewById(R.id.menu_item_icon_layout);
        iconView = (ImageView) container.findViewById(R.id.menu_item_icon);
        textView = (TextView) view.findViewById(R.id.menu_item_text);
       
    }
    
    private void loadAttrs(AttributeSet attrs) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.PostcardMenuItem);
        int image = a.getResourceId(R.styleable.PostcardMenuItem_image, 0);
        if (image > 0) {
            iconView.setImageResource(image);
        }
        type = a.getInteger(R.styleable.PostcardMenuItem_highlightType, 0);
//        if (type == 1) {
            container.setBackgroundResource(R.drawable.menu_press_selector);
//        }
            container.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                if (type == 0) {

                } else if(type == 1) {
                    selected = !selected;
                    container.setBackgroundResource(selected ? R.drawable.menu_button_press
                            : R.drawable.menu_press_selector);
                }
                
                if (onMenuClickListener != null)
                    onMenuClickListener.onClickMenu();
            }
        });
            String text = a.getString(R.styleable.PostcardMenuItem_menuText);
            if (StringUtils.isNotBlank(text)) {
                textView.setText(text);
            }
    }

    public void setOnMenuClickListener(OnMenuClickListener onMenuClickListener) {
        this.onMenuClickListener = onMenuClickListener;
    }
    
    public boolean isSelected() {
        return selected;
    }

}
